<template>
  <div class="list-index">
    <el-form :model="searchForm" :rules="searchFormRules" ref="searchForm" class="search-form" inline label-suffix=" :">
      <el-form-item label="提交时间" prop="dateRange">
        <el-date-picker type="daterange" v-model="searchForm.dateRange" range-separator="至" value-format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item label="" style="float: right">
        <el-button @click="handleSearch" type="primary" plain icon="img icon-search">查询</el-button>
      </el-form-item>
      <el-form-item label="" prop="title" style="float: right">
        <el-input v-model="searchForm.title" maxlength="10" placeholder="请输入案件名称关键字" clearable></el-input>
      </el-form-item>
    </el-form>

    <el-button @click="jumpPage" type="primary" style="margin-bottom: 20px">案件填报</el-button>

    <el-table :data="rows" v-loading="loading" stripe>
      <el-table-column prop="title" label="案件名称" show-overflow-tooltip></el-table-column>
      <el-table-column label="受理单位">
        <template slot-scope="{ row }">
          <p class="ellipsis">{{ row.acceptDepartment || "- -" }}</p>
        </template>
      </el-table-column>
      <el-table-column label="承办单位">
        <template slot-scope="{ row }">
          <p class="ellipsis">{{ row.processDepartment || "- -" }}</p>
        </template>
      </el-table-column>

      <el-table-column prop="status" label="状态" width="120px">
        <template slot-scope="{ row }">
          <span v-if="[0, 9].includes(row.status)">待受理</span>
          <span v-else-if="[1, 2, 21].includes(row.status)">已受理</span>
          <template v-else-if="row.status === 5">
            <span v-if="row.appraiseStatus === 'N'">待评价</span>
            <span v-else style="color: #35e592">已结案</span>
          </template>
          <span v-else-if="row.status === 6">不受理</span>
          <span v-else-if="row.status === 8">已终止</span>
          <span v-else-if="row.status === 10">已撤回</span>
          <span v-else-if="row.status === 12">待接收</span>
          <span v-else-if="row.status === 13">已退回</span>
          <span v-else-if="row.status === 14">放弃投诉举报</span>
          <span v-else-if="row.status === 17">修正中</span>
          <span v-else>调查处理中</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="提交时间" width="190px"></el-table-column>

      <el-table-column label="操作" width="80px">
        <template slot-scope="{ row }">
          <div>
            <el-button v-if="row.status === 17" @click="$router.push({ path: '/portals/createStep2', query: { workOrderId: row.id } })" type="primary" size="mini">编辑</el-button>
            <el-button v-else @click="$router.push({ path: '/portals/my/details', query: { id: row.id } })" type="primary" plain size="mini">查看</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="query.pageNum" :pageSize="query.pageSize" @pageChange="getWorkOrders" />
  </div>
</template>

<script>
import pagination from "@/components/pagination.vue";
import * as portalsApi from "@/api/portals";
import * as mixins from "@/utils/mixins";
import { getCMSUserInfo, postPortalsHeight } from "@/utils/common";
import { timeFormatDHMS, workOrderStatus } from "@/utils/filters";

export default {
  name: "workOrderList",
  mixins: [mixins.form],
  components: { pagination },
  filters: {
    workOrderStatus,
  },
  data() {
    return {
      loading: false,
      searchForm: {
        dateRange: [],
        // dateRange: [moment().subtract(30, "days").format("YYYY-MM-DD"), moment().format("YYYY-MM-DD")],
        title: "",
      },
      searchFormRules: {
        dateRange: [
          // {
          //   validator: (rule, value, callback) => {
          //     if (value.length === 2) {
          //       if (!moment(value[1]).isBetween(moment(value[0]).subtract(1, 'days'), moment(value[0]).add(32, 'days'), 'days')) {
          //         callback(new Error("时间跨度不可大于31天"));
          //         return
          //       }
          //     }
          //     callback()
          //   }
          // }
        ],
      },
      query: {
        pageSize: 10,
        pageNum: 1,
      },
      total: 0,
      rows: [],
    };
  },

  created() {
    console.log(this.$router, this.$route);
    // if (getCMSUserInfo().cmsAuthToken) {
    //   this.getWorkOrders()
    // }
  },
  mounted() {
    let that = this;
    // window.parent.postMessage({
    //   name: "cmsUserInfo", data: {},
    //   replay: true
    // }, "*");
    // window.onmessage = (e) => {
    //   console.log(e, "接收cms发送的cmsUserInfo");
    //   if (e.data) {
    //     if (e.data.name === "cmsUserInfo") {
    //       sessionStorage.setItem("cmsUserInfo", JSON.stringify(e.data.data));

    //     }
    //   }
    // }
    // sessionStorage.setItem('cmsUserInfo',JSON.stringify({socialCreditNumber:'',idNumber:'320911198812014678',userName:encodeURIComponent('刘基军'),userType:1,userId:1453,cmsAuthToken:'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzMjA5MTExOTg4MTIwMTQ2NzgiLCJjcmVhdGVkIjoxNzE5NTM2OTMyNjU5LCJ1c2VyU291cmNlIjoiYWRtaW4iLCJleHAiOjE3MjA0MDA5MzJ9.egKmWitzjPuTg6fahw4sm8IbdWdte5w5wnpp8crqrFHO2HRUf7lWb0lA_-bGHt8aGpzB4l_Ot3xhk6ytVwYF4w'}))
    that.getWorkOrders();
  },
  methods: {
    timeFormatDHMS,
    getWorkOrders() {
      this.loading = true;
      const form = Object.assign({}, this.searchForm, this.query);
      form.createTimeStart = form.dateRange[0];
      form.createTimeEnd = form.dateRange[1];
      const userInfo = getCMSUserInfo();
      console.log(userInfo, "userInfo------");
      form.idNumber = userInfo.idNumber;
      form.socialCreditNumber = userInfo.socialCreditNumber;
      delete form.dateRange;
      portalsApi.workOrder
        .listOfMyWorkOrder(form)
        .then((res) => {
          console.log(res, "res------");
          const result = res.result;
          this.rows = result.records;
          this.total = Number(result.total);
        })
        .then(() => this.$nextTick(() => postPortalsHeight(undefined, 350)))
        .finally(() => {
          this.loading = false;
        });
    },
    handleSearch() {
      this.query.pageNum = 1;
      this.validate("searchForm").then(() => this.getWorkOrders());
    },
    jumpPage() {
      this.$router.push(`/portals/createStep1?backPath=/portals/my/list`);
    },
  },
};
</script>

<style scoped></style>
